<%@ page contentType="text/html;charset=UTF-8" language="java"
	pageEncoding="utf-8"%>
<%@ include file="../include/head.jsp"%>
<%@ include file="../include/top.jsp"%>
<%@ include file="../include/left.jsp"%>
<div class="main-content">

	<%@ include file="../include/navigate.jsp" %>
	
	<div class="container">
		<div class="widget-box">
			<div class="widget-box-header">
				<div class="col-sm-2">
					<div class="title">
						<i class="icon-edit">&nbsp;</i>修改密码
					</div>
				</div>
			</div>
			<div class="widget-box-content" style="padding: 15px;">
				<c:if test="${not empty message}">
					<div class="alert alert-danger" role="alert">${message}</div>
				</c:if>
				<div id="saveStatus"></div>
				<form class="form-horizontal" role="form"
					action="${pageContext.request.contextPath}/user/edit" method="post">
					<input type="hidden" name="id" value="${user.id}" />
					<div class="form-group">
						<label class="col-sm-2 control-label">当前密码:</label>

						<div class="col-sm-5">
							<input type="password" class="form-control" name="oldpassword" data-toggle="tooltip" data-placement="left" required>
						</div>
					</div>

					<div class="form-group">
						<label class="col-sm-2 control-label no-padding-right"
							for="form-input-readonly"> 新密码:</label>

						<div class="col-sm-5"> 
							<input type="password" class="form-control" name="newpassword" onblur="validate()" oninput="validate()">
						</div>
						
						<div class="col-sm-2 control-label" id="password_tips" style="text-align:left;color:#D16E6C;" hidden="true" >密码长度必须大于6！</div>
										
					</div>

					<div class="form-group">
						<label class="col-sm-2 control-label">新密码确认:</label>

						<div class="col-sm-5">
							<input type="password" class="form-control" name="repassword" onblur="validConfirmPass()" oninput="validConfirmPass()"> 
						</div>
						
						<div class="col-sm-2 control-label" id="confirm_tips" style="text-align:left;color:#D16E6C;" hidden="true" >密码不匹配</div>

					</div>

					<div class="form-group">
						<label class="col-sm-2 control-label"></label>

						<div class="col-sm-2">
							<button type="button" class="btn btn-success" onclick="submitUserData()">提交</button>
							<a type="button" class="btn btn-primary"
								href="${pageContext.request.contextPath}/main"> 返回主页
							</a>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div style="position: fixed; bottom: 0px; width: 100%;" align="center">
			<%@ include file="../include/footer.jsp"%>
		</div>
	</div>


<script type="text/javascript">
	function validConfirmPass(){
		if($("input[name='newpassword']").val() != $("input[name='repassword']").val()){
			$("#confirm_tips").prop("hidden", false);
			return false;
		} else {
			$("#confirm_tips").prop("hidden", true);
			return true;
		}
	}
	
	function validate(){
		if($("input[name='newpassword']").val().length < 6){
			$('#password_tips').prop("hidden", false);
			return false;
		} else {
			$('#password_tips').prop("hidden", true);
			return true;
		}
	}
	
	window.submitUserData = function(){
		
		if(!validConfirmPass() || !validate()){
			return false;
		}
		
		$.ajax({
			type : 'post',
			url : '/user/editpwd',
			data : {
				"id" : $("input[name='id']").val(),
				"oldpassword": $("input[name='oldpassword']").val(),
				"repassword" : $("input[name='repassword']").val(),
			},
			cache : false,
			dataType : 'json',
			success : function(data) {
				if(data == 'success'){
					operationSuccess();
				} else {
					operationFailedWithMessage(data);
				}
				
			},
			error : function() {
				operationFailed();
			}
		});
	}
</script>

	<%@ include file="../include/bottom.jsp"%>